package com.hr;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.SimpleTimeZone;

public class QueryFile {
    private static String a = "E:\\新建文件夹 (3)";

    public static void main(String[] args) throws Exception {
        String time1 = "2021-09-02 00-00-00";
        String time2 = "2021-09-11 00-00-00";
        Long bb = timeBecomesMilliseconds(time1);
        Long cc = timeBecomesMilliseconds(time2);
        List<String> aa = compare(getFiles(a), bb, cc);
        for (int i = 0; i < aa.size(); i++) {
            System.out.println(aa.get(i));
            copy(aa.get(i));
        }
    }


    public static void copy(String aa) throws Exception {
        File file1 = new File(aa);
        aa = aa.replace(a, "");
        String cc = "E:\\测试" + aa;
//        String dd = cc.substring(0, cc.lastIndexOf("\\"));
////        String bb = file1.getName();
////        String dd=cc.replace(bb, "");
//        File file3 = new File(dd);
//        file3.mkdirs();
        File file2 = new File("E:\\测试" + aa);
        file2.getParentFile().mkdirs();
        try (
                BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file1));
                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file2));
        ) {
            byte[] bytes = new byte[1024];
            int x = 0;
            while ((x = bufferedInputStream.read(bytes)) != -1) {
                bufferedOutputStream.write(bytes, 0, x);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 查看获取的文件中哪些在传入的时间之间
     *
     * @param list
     * @param startTime
     * @param endTime
     * @return
     * @throws Exception
     */
    public static List<String> compare(List<String> list, Long startTime, Long endTime) throws Exception {
        List<String> list1 = new ArrayList<String>();
        for (int i = 0; i < list.size(); i++) {
            File file = new File(list.get(i));
            Long aa = file.lastModified();
            if (endTime >= aa && aa >= startTime) {
                list1.add(list.get(i));
            }
        }
        return list1;
    }

    /**
     * 把输入的时间转化成毫秒计数
     *
     * @param time
     * @return
     * @throws Exception
     */
    public static Long timeBecomesMilliseconds(String time) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        Long time1 = simpleDateFormat.parse(time).getTime();
        return time1;
    }

    /**
     * 获取到目录下所有的文件
     *
     * @param catalogue
     * @return
     */
    public static List<String> getFiles(String catalogue) {
        List<String> list1 = new ArrayList<String>();
        List<String> list2 = new ArrayList<String>();
        list1.add(catalogue);
        while (list1.size() > 0) {
            String aa = list1.remove(0);
            File file = new File(aa);
            if (file.exists()) {
                File[] files = file.listFiles();
                if (files != null && files.length > 0) {
                    for (int i = 0; i < files.length; i++) {
                        File ss = files[i];
                        if (ss.isFile()) {
                            list2.add(ss.getAbsolutePath());
                        } else {
                            list1.add(ss.getAbsolutePath());
                        }
                    }
                }
            }
        }
        return list2;
    }
}
